#!/usr/bin/env python
#
#       Effects.py
#       
#       Copyright 2009 Ben Davis <its.hip@gmail.com>, Pete Lord <mr.petelord@gmail.com>
#       
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#       
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#       
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

import math
import pygame
import random

import Bounce
import Projectiles
import Objects
import Options
import Sounds

from Client import Client
client = Client()

pygame.font.init()

class EffectsHandler():
    def __init__(self):
        self.lower = []
        self.higher = []
        
    def add(self, effect):
        if effect.r < 2 and effect.r > 5:
            self.higher.append(effect)
        else:
            self.lower.append(effect)
            
    def add_higher(self, effect):
        self.higher.append(effect)
    
    def add_lower(self, effect):
        self.lower.append(effect)
        
    def poll(self):
        for item in self.lower:
            item.poll()
            if item.life_time == 0:
                self.lower.remove(item)
                
        for item in self.higher:
            item.poll()
            if item.life_time == 0:
                self.higher.remove(item)

class Effect(Objects.Object):
    def __init__(self, parent, r, life_time, surface):
        Objects.Object.__init__(self, parent.x, parent.y, r)
        self.x = parent.x
        self.y = parent.y
        self.r = r 
        self.max_life_time = self.life_time = life_time
        self.surfaces = surface
        
    def draw(self):
        # the Ship class should _never_ be instantiated, this funcition will not work without self.textures
        # which is defined as part of the ship class itself.
        return self.surfaces[Options.ZOOM_LEVEL][0][self.r][self.current_frame], pygame.Rect(self.screen_location(), (Options.SPRITE_SIZE[Options.ZOOM_LEVEL], Options.SPRITE_SIZE[Options.ZOOM_LEVEL]))    
        
    def poll(self):
        self.life_time -= 1
        # Removal of effects handled by EffectsHandler
        for surface in self.surfaces[Options.ZOOM_LEVEL][0][self.r]:
            surface.set_alpha(255 * self.life_time / self.max_life_time)
            
class PawnCannonA(Effect):
    def __init__(self, parent, surface):
        self.anim_frames = 7
        self.anim_frame_times = 5 
        Effect.__init__(self, parent, parent.r, self.anim_frames * self.anim_frame_times, surface)
        
        # basic effect.
        self.anim_frames = 7
        self.anim_frame_times = 5 
        self.has_anim = True
        self.current_time = 0
        self.current_frame = 0 # init.
        
        # play sound
        client.sounds.sfx('steamcannons.ogg')
        
    def poll(self):
        self.life_time -= 1
        # Removal of effects handled by EffectsHandler
        for surface in self.surfaces[Options.ZOOM_LEVEL][0][self.r]:
            surface.set_alpha(255 * self.life_time / self.max_life_time)
            
        self.current_time += 1
        if self.current_time == self.anim_frame_times:
            self.current_time = 0
            self.current_frame += 1
            
class PawnCannonB(PawnCannonA):
    def __init__(self, parent, surface):
        PawnCannonA.__init__(self, parent, surface)
        
class KingCannonS(PawnCannonA):
    def __init__(self, parent, surface):
        PawnCannonA.__init__(self, parent, surface)
        
class KingCannonW(PawnCannonA):
    def __init__(self, parent, surface):
        #particle waterz
        PawnCannonA.__init__(self, parent, surface)
        
class ShipHit(Objects.Object):
    def __init__(self, parent):
        Objects.Object.__init__(self, parent.x, parent.y, 3)
        self.life_time = 10
        self.colour = (255, 0, 0)
        self.alpha = 150
        self.surface, self.position = parent.draw()
        self.poll()
        
    def poll(self):
        # Create a copy of it's sprite
        self.mask = self.surface.copy()
        # Create a red mask of the non-transparent areas
        self.mask.fill(self.colour, None, pygame.BLEND_ADD)
        # Lower the alpha levels
        self.mask.fill(self.colour + (self.alpha,), None, pygame.BLEND_RGBA_MULT)
        self.alpha -= 5
        self.life_time -= 1
        
    def draw(self):
        return (self.mask, self.position)    
        
class ShipHitCritical(Objects.Object):
    def __init__(self, parent):
        Objects.Object.__init__(self, parent.x, parent.y, 3)
        self.life_time = 10
        self.colour = (200, 200, 0)
        self.alpha = 255
        self.surface, self.position = parent.draw()
        self.poll()
        
    def poll(self):
        # Create a copy of it's sprite
        self.mask = self.surface.copy()
        # Create a red mask of the non-transparent areas
        self.mask.fill(self.colour, None, pygame.BLEND_ADD)
        # Lower the alpha levels
        self.mask.fill(self.colour + (self.alpha,), None, pygame.BLEND_RGBA_MULT)
        self.alpha -= 5
        self.life_time -= 1
        
    def draw(self):
        return (self.mask, self.position)  
        
class ShipDeath(Objects.Object):
    def __init__(self, parent):
        Objects.Object.__init__(self, parent.x, parent.y, 3)
        self.life_time = 120
        self.colour = (0, 0, 0)
        self.alpha = 240
        self.surface, self.position = parent.draw()
        self.poll()
        
    def poll(self):
        # Create a copy of it's sprite
        self.mask = self.surface.copy()
        # Create a red mask of the non-transparent areas
        self.mask.fill(self.colour, None, pygame.BLEND_ADD)
        # Lower the alpha levels
        self.mask.fill(self.colour + (self.alpha,), None, pygame.BLEND_RGBA_MULT)
        self.alpha -= 2
        self.life_time -= 1 
        
    def draw(self):
        return (self.mask, self.position)     
            
class FloatyText(Effect):
    font = pygame.font.Font(pygame.font.match_font('georgia'), 12)
    def __init__(self, colour, text, ship):
        self.font.set_bold(True)
        self.surface = self.font.render(text, True, colour)
        Effect.__init__(self, ship, 3, 20, self.surface)
        self.maxlife = self.life_time
        self.xy_mod.push(10, 3)
        
    def poll(self):
        self.life_time -= 1
        if self.life_time % 6 == 0:
            self.xy_mod.poll()
        #self.surface.set_alpha(255 * self.life_time / self.maxlife)
        #print self.surface.get_alpha()

    def draw(self):
        x, y = self.screen_location()
        x += (Options.TILE_WIDTH[Options.ZOOM_LEVEL] - self.surface.get_width()) / 2
        y += (Options.TILE_HEIGHT[Options.ZOOM_LEVEL] - self.surface.get_height()) / 2
        return self.surface, pygame.Rect((x, y - 10), self.surface.get_size())

